<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>mainwindow.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-richtext-calendar-example.html">Calendar Example</a></td><td >mainwindow.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">mainwindow.cpp 示例文件</h1>
<span class="subtitle">richtext/calendar/mainwindow.cpp</span>
<!-- $$$richtext/calendar/mainwindow.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &quot;mainwindow.h&quot;</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  MainWindow<span class="operator">::</span>MainWindow()
  {
      selectedDate <span class="operator">=</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span><span class="operator">::</span>currentDate();
      fontSize <span class="operator">=</span> <span class="number">10</span>;

      <span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>centralWidget <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qwidget.html">QWidget</a></span>;

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>dateLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Date:&quot;</span>));
      <span class="type"><a href="qcombobox.html">QComboBox</a></span> <span class="operator">*</span>monthCombo <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qcombobox.html">QComboBox</a></span>;

      <span class="keyword">for</span> (<span class="type">int</span> month <span class="operator">=</span> <span class="number">1</span>; month <span class="operator">&lt;</span><span class="operator">=</span> <span class="number">12</span>; <span class="operator">+</span><span class="operator">+</span>month)
          monthCombo<span class="operator">-</span><span class="operator">&gt;</span>addItem(<span class="type"><a href="../qtcore/qlocale.html">QLocale</a></span><span class="operator">::</span>system()<span class="operator">.</span>monthName(month));

      <span class="type"><a href="qdatetimeedit.html">QDateTimeEdit</a></span> <span class="operator">*</span>yearEdit <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qdatetimeedit.html">QDateTimeEdit</a></span>;
      yearEdit<span class="operator">-</span><span class="operator">&gt;</span>setDisplayFormat(<span class="string">&quot;yyyy&quot;</span>);
      yearEdit<span class="operator">-</span><span class="operator">&gt;</span>setDateRange(<span class="type"><a href="../qtcore/qdate.html">QDate</a></span>(<span class="number">1753</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>)<span class="operator">,</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span>(<span class="number">8000</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">1</span>));

      monthCombo<span class="operator">-</span><span class="operator">&gt;</span>setCurrentIndex(selectedDate<span class="operator">.</span>month() <span class="operator">-</span> <span class="number">1</span>);
      yearEdit<span class="operator">-</span><span class="operator">&gt;</span>setDate(selectedDate);

      <span class="type"><a href="qlabel.html">QLabel</a></span> <span class="operator">*</span>fontSizeLabel <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlabel.html">QLabel</a></span>(tr(<span class="string">&quot;Font size:&quot;</span>));
      <span class="type"><a href="qspinbox.html">QSpinBox</a></span> <span class="operator">*</span>fontSizeSpinBox <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qspinbox.html">QSpinBox</a></span>;
      fontSizeSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setRange(<span class="number">1</span><span class="operator">,</span> <span class="number">64</span>);

      editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtextbrowser.html">QTextBrowser</a></span>;
      insertCalendar();

      connect(monthCombo<span class="operator">,</span> SIGNAL(activated(<span class="type">int</span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(setMonth(<span class="type">int</span>)));
      connect(yearEdit<span class="operator">,</span> SIGNAL(dateChanged(<span class="type"><a href="../qtcore/qdate.html">QDate</a></span>))<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(setYear(<span class="type"><a href="../qtcore/qdate.html">QDate</a></span>)));
      connect(fontSizeSpinBox<span class="operator">,</span> SIGNAL(valueChanged(<span class="type">int</span>))<span class="operator">,</span>
              <span class="keyword">this</span><span class="operator">,</span> SLOT(setFontSize(<span class="type">int</span>)));

      fontSizeSpinBox<span class="operator">-</span><span class="operator">&gt;</span>setValue(<span class="number">10</span>);

      <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span> <span class="operator">*</span>controlsLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qhboxlayout.html">QHBoxLayout</a></span>;
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(dateLabel);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(monthCombo);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(yearEdit);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addSpacing(<span class="number">24</span>);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(fontSizeLabel);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(fontSizeSpinBox);
      controlsLayout<span class="operator">-</span><span class="operator">&gt;</span>addStretch(<span class="number">1</span>);

      <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span> <span class="operator">*</span>centralLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qvboxlayout.html">QVBoxLayout</a></span>;
      centralLayout<span class="operator">-</span><span class="operator">&gt;</span>addLayout(controlsLayout);
      centralLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(editor<span class="operator">,</span> <span class="number">1</span>);
      centralWidget<span class="operator">-</span><span class="operator">&gt;</span>setLayout(centralLayout);

      setCentralWidget(centralWidget);
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>insertCalendar()
  {
      editor<span class="operator">-</span><span class="operator">&gt;</span>clear();
      <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cursor <span class="operator">=</span> editor<span class="operator">-</span><span class="operator">&gt;</span>textCursor();
      cursor<span class="operator">.</span>beginEditBlock();

      <span class="type"><a href="../qtcore/qdate.html">QDate</a></span> date(selectedDate<span class="operator">.</span>year()<span class="operator">,</span> selectedDate<span class="operator">.</span>month()<span class="operator">,</span> <span class="number">1</span>);

      <span class="type"><a href="../qtgui/qtexttableformat.html">QTextTableFormat</a></span> tableFormat;
      tableFormat<span class="operator">.</span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignHCenter);
      tableFormat<span class="operator">.</span>setBackground(<span class="type"><a href="../qtgui/qcolor.html">QColor</a></span>(<span class="string">&quot;#e0e0e0&quot;</span>));
      tableFormat<span class="operator">.</span>setCellPadding(<span class="number">2</span>);
      tableFormat<span class="operator">.</span>setCellSpacing(<span class="number">4</span>);
      <span class="type"><a href="../qtcore/qvector.html">QVector</a></span><span class="operator">&lt;</span><span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">&gt;</span> constraints;
      constraints <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>)
                  <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span>(<span class="type"><a href="../qtgui/qtextlength.html">QTextLength</a></span><span class="operator">::</span>PercentageLength<span class="operator">,</span> <span class="number">14</span>);
      tableFormat<span class="operator">.</span>setColumnWidthConstraints(constraints);

      <span class="type"><a href="../qtgui/qtexttable.html">QTextTable</a></span> <span class="operator">*</span>table <span class="operator">=</span> cursor<span class="operator">.</span>insertTable(<span class="number">1</span><span class="operator">,</span> <span class="number">7</span><span class="operator">,</span> tableFormat);

      <span class="type"><a href="../qtgui/qtextframe.html">QTextFrame</a></span> <span class="operator">*</span>frame <span class="operator">=</span> cursor<span class="operator">.</span>currentFrame();
      <span class="type"><a href="../qtgui/qtextframeformat.html">QTextFrameFormat</a></span> frameFormat <span class="operator">=</span> frame<span class="operator">-</span><span class="operator">&gt;</span>frameFormat();
      frameFormat<span class="operator">.</span>setBorder(<span class="number">1</span>);
      frame<span class="operator">-</span><span class="operator">&gt;</span>setFrameFormat(frameFormat);

      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> format <span class="operator">=</span> cursor<span class="operator">.</span>charFormat();
      format<span class="operator">.</span>setFontPointSize(fontSize);

      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> boldFormat <span class="operator">=</span> format;
      boldFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="../qtgui/qfont.html">QFont</a></span><span class="operator">::</span>Bold);

      <span class="type"><a href="../qtgui/qtextcharformat.html">QTextCharFormat</a></span> highlightedFormat <span class="operator">=</span> boldFormat;
      highlightedFormat<span class="operator">.</span>setBackground(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>yellow);

      <span class="keyword">for</span> (<span class="type">int</span> weekDay <span class="operator">=</span> <span class="number">1</span>; weekDay <span class="operator">&lt;</span><span class="operator">=</span> <span class="number">7</span>; <span class="operator">+</span><span class="operator">+</span>weekDay) {
          <span class="type"><a href="../qtgui/qtexttablecell.html">QTextTableCell</a></span> cell <span class="operator">=</span> table<span class="operator">-</span><span class="operator">&gt;</span>cellAt(<span class="number">0</span><span class="operator">,</span> weekDay<span class="operator">-</span><span class="number">1</span>);
          <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cellCursor <span class="operator">=</span> cell<span class="operator">.</span>firstCursorPosition();
          cellCursor<span class="operator">.</span>insertText(<span class="type"><a href="../qtcore/qlocale.html">QLocale</a></span><span class="operator">::</span>system()<span class="operator">.</span>dayName(weekDay)<span class="operator">,</span> boldFormat);
      }

      table<span class="operator">-</span><span class="operator">&gt;</span>insertRows(table<span class="operator">-</span><span class="operator">&gt;</span>rows()<span class="operator">,</span> <span class="number">1</span>);

      <span class="keyword">while</span> (date<span class="operator">.</span>month() <span class="operator">=</span><span class="operator">=</span> selectedDate<span class="operator">.</span>month()) {
          <span class="type">int</span> weekDay <span class="operator">=</span> date<span class="operator">.</span>dayOfWeek();
          <span class="type"><a href="../qtgui/qtexttablecell.html">QTextTableCell</a></span> cell <span class="operator">=</span> table<span class="operator">-</span><span class="operator">&gt;</span>cellAt(table<span class="operator">-</span><span class="operator">&gt;</span>rows()<span class="operator">-</span><span class="number">1</span><span class="operator">,</span> weekDay<span class="operator">-</span><span class="number">1</span>);
          <span class="type"><a href="../qtgui/qtextcursor.html">QTextCursor</a></span> cellCursor <span class="operator">=</span> cell<span class="operator">.</span>firstCursorPosition();

          <span class="keyword">if</span> (date <span class="operator">=</span><span class="operator">=</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span><span class="operator">::</span>currentDate())
              cellCursor<span class="operator">.</span>insertText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;%1&quot;</span>)<span class="operator">.</span>arg(date<span class="operator">.</span>day())<span class="operator">,</span> highlightedFormat);
          <span class="keyword">else</span>
              cellCursor<span class="operator">.</span>insertText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;%1&quot;</span>)<span class="operator">.</span>arg(date<span class="operator">.</span>day())<span class="operator">,</span> format);

          date <span class="operator">=</span> date<span class="operator">.</span>addDays(<span class="number">1</span>);
          <span class="keyword">if</span> (weekDay <span class="operator">=</span><span class="operator">=</span> <span class="number">7</span> <span class="operator">&amp;</span><span class="operator">&amp;</span> date<span class="operator">.</span>month() <span class="operator">=</span><span class="operator">=</span> selectedDate<span class="operator">.</span>month())
              table<span class="operator">-</span><span class="operator">&gt;</span>insertRows(table<span class="operator">-</span><span class="operator">&gt;</span>rows()<span class="operator">,</span> <span class="number">1</span>);
      }

      cursor<span class="operator">.</span>endEditBlock();
      setWindowTitle(tr(<span class="string">&quot;Calendar for %1 %2&quot;</span>
          )<span class="operator">.</span>arg(<span class="type"><a href="../qtcore/qlocale.html">QLocale</a></span><span class="operator">::</span>system()<span class="operator">.</span>monthName(selectedDate<span class="operator">.</span>month())
          )<span class="operator">.</span>arg(selectedDate<span class="operator">.</span>year()));
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setFontSize(<span class="type">int</span> size)
  {
      fontSize <span class="operator">=</span> size;
      insertCalendar();
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setMonth(<span class="type">int</span> month)
  {
      selectedDate <span class="operator">=</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span>(selectedDate<span class="operator">.</span>year()<span class="operator">,</span> month <span class="operator">+</span> <span class="number">1</span><span class="operator">,</span> selectedDate<span class="operator">.</span>day());
      insertCalendar();
  }

  <span class="type">void</span> MainWindow<span class="operator">::</span>setYear(<span class="type"><a href="../qtcore/qdate.html">QDate</a></span> date)
  {
      selectedDate <span class="operator">=</span> <span class="type"><a href="../qtcore/qdate.html">QDate</a></span>(date<span class="operator">.</span>year()<span class="operator">,</span> selectedDate<span class="operator">.</span>month()<span class="operator">,</span> selectedDate<span class="operator">.</span>day());
      insertCalendar();
  }

</pre>
</div>
<!-- @@@richtext/calendar/mainwindow.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
